// $Id$
package de.servicezombie.pub.r1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
		"key", "args", "text"
})
@XmlRootElement(name = "message", namespace = XmlNamespaces.SERVICEZOMBIE_COMMON)
public class Message {

	/**
	 * a common used message key
	 */
	@XmlElement(required = true)
	private String key;

	/**
	 * values to be inserted in the text. do not use object 
	 * here to avoid w3 - string
	 */
	@XmlElement(required = false)
	private List<String> args;

	/**
	 * the already translated text. may not be available.
	 */
	@XmlElement(required = false)
	private String text;

	public Message(String key, String text, Object... args) {
		this.key = key;
		this.text = text;
		this.args = toStringList(args);	
	}
	
	public Message() {
		this.key = null;
		this.text = null;
		this.args = Collections.emptyList();	
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public List<String> getArgsAsString() {
		return args;
	}

	public void setArgs(Object... args) {
		this.args = toStringList(args);
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}
	
	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
				.append("key", key)
				.append("args", args)
				.append("isTranslated", text != null)
				.toString();
	}
	
	private List<String> toStringList(Object... items) {
		if(items == null) {
			return Collections.emptyList();
		}
		
		ArrayList<String> result = new ArrayList<String>(items.length);
		for(Object item : items) {
			result.add(item == null ? StringUtils.EMPTY : item.toString());
		}
		return result;
	}

}
